package com.recognition.webcam;

import java.awt.image.BufferedImage;

import com.lti.civil.CaptureException;
import com.lti.civil.CaptureObserver;
import com.lti.civil.CaptureStream;
import com.lti.civil.Image;
import com.lti.civil.awt.AWTImageConverter;
import com.recognition.Logger;
import com.recognition.Main;
import com.recognition.image.SnapshotManager;

class SnapshotObserver implements CaptureObserver
{
	private static Logger _log = Logger.getLogger();
	private Webcam _webcam;
	private BufferedImage _snapshot;
	private boolean _captureReference = false, _capturePicture = false;
	
	public SnapshotObserver(Webcam webcam)
	{
		_webcam = webcam;
	}
	
	public void onError(CaptureStream sender, CaptureException e)
	{	
		_log.error("Observer: " + sender);
		_log.error("Observer: " + e.getMessage());
	}

	public void onNewImage(CaptureStream sender, Image image)
	{	
		_snapshot = AWTImageConverter.toBufferedImage(image);	
		_webcam.updateFrame(_snapshot);
		if (_captureReference)
		{			
			_captureReference = false;			
			SnapshotManager.getManager().setReference(_snapshot);
			_log.info("Observer: Created reference picture");
		}
		else if (_capturePicture)
		{
			_capturePicture = false;			
			SnapshotManager.getManager().setImage(_snapshot);
			_log.info("Observer: Created picture");
		}
	}
	
	public void capturePicture(boolean reference)
	{
		_captureReference = reference;
		_capturePicture = !reference;
	}
}